package top.iridescent.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * 数据库操作工具类
 */
@Slf4j
public class DatabaseUtils {

    public static Connection getDbConnection(String driverName, String url, String userName, String password) {
        //加载驱动
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            log.error("驱动不存在!");
            e.printStackTrace();
        }
        //获取数据库连接
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, userName, password);
        } catch (SQLException throwables) {
            log.error("数据库连接失败! url : {} ,userName : {} ,password : {}", url, userName, password);
            throwables.printStackTrace();
        }
        return conn;
    }

    public static boolean insertMsg(Connection conn, String sql) {
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            return ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

